package com.mengya.completablefuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * @ClassName FutureTaskDemo
 * @Description
 * @Author xiongwei.wu
 * @Date 2022/11/15 15:50
 **/
public class FutureTaskDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
//        Callable

        FutureTask<Integer> task = new FutureTask<>(() -> {
            System.out.println(Thread.currentThread().getName() + " \t " + "------come in");
            TimeUnit.SECONDS.sleep(3);
            return 1024;
        });

        new Thread(task, "t1").start();

        //不见不散，只要出现get方法，不管是否计算完成都阻塞等待结果出来再运行
//        System.out.println(task.get());

        //过期不候
//        System.out.println(task.get(2L, TimeUnit.SECONDS));

        while (true) {
            if (task.isDone()) {
                System.out.println("-------result: " + task.get());
                break;
            } else {
                System.out.println("还在计算中，别催，越催越慢");
            }
        }

//        System.out.println("杨老板继续讲课....");

    }
}
